package com.jiaai.shop.util;



import org.springframework.boot.test.context.SpringBootTest;

import java.io.*;
import java.util.ArrayList;
import java.util.List;


@SpringBootTest
public class ShellTest {

    public   static   void   main   (String   args[])   {
//        //要读取的文件位置
//        String filepath="E:\\wenjianceshi\\文件所在地\\application.properties";
//        //要写入的位置
//        String tofilepath="E:\\wenjianceshi\\文件到达地\\application.properties";
//        //文件中的占位符{名字}  按顺序
//        List<String> items=new ArrayList<>();
//        items.add(0,"\\{username}");
//        items.add(1,"\\{password}");
//        //替换后的内容  按顺序
//        List<String > data=new ArrayList<>();
//        data.add(0,"892150407@qq.com");
//        data.add(1,"kjnvrhspfsdfbedb");
//        changeFileText(filepath,tofilepath,items,data);
        runShell();
    }
    /*
        *
        * @Author:wangyuheng
        * filepath 模板文件路径
        * tofilepath 要生成的文件路径
        * items word xml模板文件中的占位符
        * data  word xml文件要替换的数据
        */
    public static boolean changeFileText(String filepath, String tofilepath, List<String> items, List<String > data){
        File file = new File(filepath);
        String line=null;
        InputStream is=null;
        FileOutputStream fos=null;
        int i=0;
        try{
            is = new FileInputStream(file);
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            File out=new File(tofilepath);
            fos=new FileOutputStream(out);
            while((line=reader.readLine()) != null){
                if(line.contains("{")) {
                    line = line.replaceAll(items.get(i), data.get(i));
                    i++;
                }
                    line=line+"\r\n";
                fos.write(line.getBytes());
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }finally{
            try {
                is.close();
                fos.close();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }
public static void runShell(){
    try {
        String shpath="E:\\wyh\\demo-classes.sh";
        Process ps = Runtime.getRuntime().exec(shpath);
        ps.waitFor();

        BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
        StringBuffer sb = new StringBuffer();
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line).append("\n");
        }
        String result = sb.toString();
        System.out.println(result);
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}
}
